<template>
  <div class="create-brand">
    <el-form ref="form" :model="brand" :rules="rules" label-width="100px" size="small">
      <el-form-item label="品牌名称" prop="brandName">
        <el-input v-model="brand.brandName" auto-complete="off" placeholder="请输入产品名称"></el-input>
      </el-form-item>
      <el-form-item label="品牌简介" prop="remarks">
        <el-input
          v-model="brand.remarks"
          type="textarea"
          :rows="4"
          auto-complete="off"
          placeholder="请输入品牌简介"
        ></el-input>
      </el-form-item>
    </el-form>
    <div class="operate-button">
      <el-button @click="handleCancel" size="small">取 消</el-button>
      <el-button type="primary" @click="handleConfirm" size="small">确 定</el-button>
    </div>
  </div>
</template>
<script>
import { createBrand } from "@/api/brand";
export default {
  name: "Brand",
  data() {
    return {
      brand: {
        brandName: "",
        remarks: "",
      },
      rules: {
        brandName: [{ required: true, message: "请输入产品名称", trigger: "blur" }]
      },
    };
  },
  created() {
  },
  methods: {
    handleCancel() {
      this.$emit("after-create", false);
    },
    handleConfirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const data = {...this.brand}
          createBrand(data).then(res => {
            if (res.code === 200) {
              this.$message({
                message: "新建成功",
                type: "success",
                duration: 1000
              });
              this.$emit("after-create", true);
            } else if (res.code === 202) {
              this.$message({
                message: "品牌已经存在",
                type: "error",
                duration: 1000
              });
            } else {
              this.$message({
                message: "新建失败",
                type: "error",
                duration: 1000
              });
            }
          });
        }
      });
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.create-brand {
  .el-form-item {
    width: 95%;
    .el-select {
      width: 100%;
    }
  }
}
.operate-button {
  text-align: center;
}
</style>


